#!/bin/bash
#############################################################
# <next few lines under version control, D O  N O T  E D I T>
# $Date: 2010-11-05 23:01:51 +0000 (Fri, 05 Nov 2010) $
# $Author: rahulm $
# $Revision: 36 $
# $Id: create_control.scr 36 2010-11-05 23:01:51Z rahulm $
#############################################################

#############################################################
# F U N C T I O N S   S T A R T   H E R E
#############################################################
clean_on_exit( ) {
	logfile=$1
	exp_name=$2
	\rm -f /tmp/msg
	echo "last 50 lines of ... ${logfile}" >> /tmp/msg
	tail -n 50 ${logfile}                  >> /tmp/msg
	mutt -s "${exp_name} has stopped on host `hostname -s`" `whoami`@atmos.uw.edu < /tmp/msg
	\rm -f /tmp/msg
	\rm -f sent
	exit
}

#############################################################
# M A I N   S C R I P T   S T A R T S   H E R E
#############################################################

param=${1}
until [ -e ${param} ]; do sleep 10; done
source ${param}

# define experiment source, run and log directories 
dir_src=${dir_expt_src}/${exp_name}/${run_type}/src
dir_run=${dir_expt_run}/${exp_name}/${run_type}/run${run_number}
dir_run_truth=${dir_expt_run}/${exp_name}/truth/run${truth_run_number}

# create run directories 
if [ -e ${dir_run} ]; then
	tmplog=/tmp/logfile
	\rm -f ${tmplog}; touch ${tmplog}
	echo "   ################################################"   >> ${tmplog}
	echo "   ${dir_run} exists"                                  >> ${tmplog}
	echo "   cannot continue, needs your attention, EXITING  "   >> ${tmplog}
	echo "   ################################################"   >> ${tmplog}
  echo "                                                   "   >> ${tmplog}
	clean_on_exit ${tmplog} ${exp_name}/${run_type}
else
	mkdir -p ${dir_run}
fi

logfile=${dir_run}/log_${run_type}_${run_number}
touch ${logfile}

echo "                                                             " >> ${logfile}
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >> ${logfile}
echo " ${run_type} for experiment ${exp_name}                      " >> ${logfile}
echo " started at `date`                                           " >> ${logfile}
echo " on host `hostname -s`                                       " >> ${logfile}
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >> ${logfile}
echo "                                                             " >> ${logfile}

# Change to working directory
cd ${dir_run}

# Copy base.nc and Link smat.nc from ${dir_run_truth}
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >> ${logfile}
echo "Copy ${dir_run_truth}/base.nc"                                 >> ${logfile}
if [ -e ${dir_run_truth}/base.nc ]; then
  \cp -f ${dir_run_truth}/base.nc    .
else
	echo "   ################################################" >> ${logfile}
  echo "   ${dir_run_truth}/base.nc needs to exist"          >> ${logfile}
	echo "   ################################################" >> ${logfile}
	clean_on_exit ${logfile} ${exp_name}/${run_type}
fi
echo "Link ${dir_run_truth}/smat.nc"                                 >> ${logfile}
if [ -e ${dir_run_truth}/smat.nc ]; then
  \ln -sf ${dir_run_truth}/smat.nc    .
	ncdump -h smat.nc >> ${logfile} 2>&1
else
	echo "   ################################################" >> ${logfile}
  echo "   ${dir_run_truth}/smat.nc needs to exist"          >> ${logfile}
	echo "   ################################################" >> ${logfile}
	clean_on_exit ${logfile} ${exp_name}/${run_type}
fi
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >> ${logfile}

# Extract control ensemble members from truth
echo "                                                             " >> ${logfile}
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >> ${logfile}
echo "Extract Control from Truth"                                    >> ${logfile}
let ie=1
let tstart='(ie - 1) * (tint - 1) + 1'
let tend='tstart + tint - 1'
while [ $ie -le $Ne ]; do
	echo "   ################################################" >> ${logfile}
	let emem='100000 + ie'
	emem=`echo ${emem} | cut -b2-`
	echo "   Member ${emem}, ${tstart} --> ${tend}"                     >> ${logfile}
	ncks -F -h -d time,${tstart},${tend},1 smat.nc -o smat_C_${emem}.nc >> ${logfile} 2>&1
	ncatted -h -O -a ntims,global,d,,                 smat_C_${emem}.nc >> ${logfile} 2>&1
	if [ ! -e smat_C_${emem}.nc ]; then
		echo "   ncks OR ncatted FAILED on member ${emem}"                >> ${logfile}
		clean_on_exit ${logfile} ${exp_name}/${run_type}
	fi
	let tstart='tend'
	let tend='tstart + tint - 1'
	echo "   ################################################" >> ${logfile}
	let ie='ie + 1'
done
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >> ${logfile}
echo "                                                             " >> ${logfile}

# Clean up and remove the linked smat.nc file
\rm -f ${dir_run_truth}/smat.nc    .

# Combine all smat_C_XXXXX.nc into smat_C_diag.nc
\cp -f ${dir_src}/fortran/Makefile     .
\cp -f ${dir_src}/fortran/sqg_diag.f90 .

make clean    >> ${logfile} 2>&1
make sqg_diag >> ${logfile} 2>&1

if [ ! -e sqg_diag ]; then
	echo "   sqg_diag did not get compiled correctly" >> ${logfile}
	clean_on_exit ${logfile} ${exp_name}/${run_type}
fi

\rm -f control.diag.*
cat >> control.diag.in << END_INPUT
${Ne}
'${dir_run}/'
'C'
END_INPUT

(time ./sqg_diag < control.diag.in) > control.diag.out 2>&1
cat control.diag.out >> ${logfile}

let success=`grep "sqg_diag done" control.diag.out | wc -l`
if [ $success -eq 0 ]; then
	echo "   sqg_diag failed to create smat_C_diag.nc" >> ${logfile}
	clean_on_exit ${logfile} ${exp_name}/${run_type}
else
	\rm -f control.diag.*
	while [ $ie -le $Ne ]; do
		let emem='100000 + ie'
		emem=`echo ${emem} | cut -b2-`
		\rm -f smat_C_${emem}.nc
		let ie='ie + 1'
	done
fi

echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >> ${logfile}
echo " ${run_type} for experiment ${exp_name}"                       >> ${logfile}
echo " completed at `date`"                                          >> ${logfile}
echo " on host `hostname -s`"                                        >> ${logfile}
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >> ${logfile}

# Upon Exit, send email and clean
clean_on_exit ${logfile} ${exp_name}/${run_type}
